package com.dongdongshop.service.impl;

import cn.hutool.core.util.RandomUtil;
import com.aliyun.sdk.service.dysmsapi20170525.AsyncClient;
import com.aliyun.sdk.service.dysmsapi20170525.models.SendSmsRequest;
import com.aliyun.sdk.service.dysmsapi20170525.models.SendSmsResponse;
import com.dongdongshop.admin.model.TbUser;
import com.dongdongshop.admin.model.TbUserExample;
import com.dongdongshop.admin.vo.TbUserVO;
import com.dongdongshop.mapper.TbUserMapper;
import com.dongdongshop.service.TUserservice;
import com.dongdongshop.shiro.ShiroUtils;
import com.google.gson.Gson;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.function.Function;
import java.util.stream.Collectors;

@Service
public class TUserserviceImpl implements TUserservice {
    @Autowired
    private TbUserMapper tbUserMapper;

    @Override
    public TbUser findTbUserByName(String username) {
        TbUserExample example = new TbUserExample();
        TbUserExample.Criteria criteria = example.createCriteria();
        criteria.andUsernameEqualTo(username);
        List<TbUser> users = tbUserMapper.selectByExample(example);
        return users ==null || users.isEmpty() ? null: users.get(0);
    }

    @Override
    public List<TbUserVO> getAll() {
        List<TbUser> tbUsers = tbUserMapper.selectByExample(null);
        return tbUsers.stream().map(i-> {
            TbUserVO vo = new TbUserVO();
            BeanUtils.copyProperties(i, vo);
            return vo;
        }).collect(Collectors.toList());
    }

    @Override
    public void addTbUser(TbUserVO tbUserVO) {
        //创建对象
        TbUser tbUser = new TbUser();
        //将数据传输到容器中
        BeanUtils.copyProperties(tbUserVO, tbUser);
        //创建盐
        String salt = ShiroUtils.generateSalt(6);
        tbUser.setSalt(salt);
        //创建加密算法
        String md5 = ShiroUtils.encryptPassword("MD5", tbUser.getPassword(), tbUser.getSalt(),10);
        tbUser.setPassword(md5);
        tbUser.setCreated(new Date());
        tbUser.setUpdated(new Date());
        tbUser.setStatus("Y");
        tbUserMapper.insertSelective(tbUser);
    }

    @Override
    public TbUserVO getTbUserByName(String username) {
        TbUserExample example = new TbUserExample();
        TbUserExample.Criteria criteria = example.createCriteria();
        criteria.andUsernameEqualTo(username);
        List<TbUser> users = tbUserMapper.selectByExample(example);
        List<TbUserVO> collect = users.stream().map(m -> {
            TbUserVO vo = new TbUserVO();
            BeanUtils.copyProperties(m, vo);
            return vo;
        }).collect(Collectors.toList());
        return collect ==null || collect.isEmpty() ? null: collect.get(0);
    }

    /*@Override
    public void sendPhoneMessage(String phoneNum) {
        String s = RandomUtil.randomNumbers(6);
        // Parameter settings for API request
        SendSmsRequest sendSmsRequest = SendSmsRequest.builder()
                .signName("东科创想")
                .templateCode("SMS_162522027")
                .templateParam("{\"sms_code\":\""+s+"\"}")
                .phoneNumbers(phoneNum)
                .build();

        // Asynchronously get the return value of the API request
        CompletableFuture<SendSmsResponse> response = client.sendSms(sendSmsRequest);
        // Synchronously get the return value of the API request
        SendSmsResponse resp = null;
        try {
            resp = response.get();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
        System.out.println(new Gson().toJson(resp));
        //如果操作成功将验证码存入redis并设置过期时间
        redisTemplate.boundValueOps(phoneNum).set(s);
        //设置过期时间30分钟
        redisTemplate.expire(phoneNum,30, TimeUnit.MINUTES);
    }*/
}
